﻿package mortal.game.view.common
{
    import com.gengine.core.frame.*;
    import com.mui.controls.*;

    public class SecTimerButton extends GButton
    {
        private var _secTimer:SecTimer;
        private var _callback:Function;
        private var _labelStr:String;

        public function SecTimerButton()
        {
            return;
        }// end function

        override public function set label(param1:String) : void
        {
            super.label = param1;
            this._labelStr = param1;
            return;
        }// end function

        public function startCountdown(param1:int, param2:Function = null, param3:Boolean = true) : void
        {
            if (param2 != null)
            {
                this._callback = param2;
            }
            if (this._secTimer == null)
            {
                this._secTimer = new SecTimer();
                this._secTimer.addListener(TimerType.COMPLETE, this.onTimerCompleteHandler);
                this._secTimer.addListener(TimerType.ENTERFRAME, this.onEnterFrameHandler);
            }
            else
            {
                this._secTimer.reset();
            }
            this._secTimer.repeatCount = param1;
            if (param3)
            {
                this._secTimer.start();
            }
            return;
        }// end function

        public function reset() : void
        {
            if (this._secTimer)
            {
                this._secTimer.reset();
                this._secTimer.repeatCount = 0;
            }
            if (this._labelStr != null)
            {
                super.label = this._labelStr;
            }
            return;
        }// end function

        public function stop() : void
        {
            if (this._secTimer)
            {
                return this._secTimer.stop();
            }
            if (this._labelStr != null)
            {
                super.label = this._labelStr;
            }
            return;
        }// end function

        public function get running() : Boolean
        {
            if (this._secTimer)
            {
                return this._secTimer.running;
            }
            return false;
        }// end function

        public function set timeOutHandler(param1:Function) : void
        {
            this._callback = param1;
            return;
        }// end function

        private function onTimerCompleteHandler(param1:SecTimer) : void
        {
            if (this._callback != null)
            {
                this._callback.apply();
            }
            this.stop();
            return;
        }// end function

        private function onEnterFrameHandler(param1:SecTimer) : void
        {
            super.label = this._labelStr + "(" + param1.repeatCount + ")";
            if (param1.repeatCount <= 0 && this._labelStr != null)
            {
                super.label = this._labelStr;
            }
            return;
        }// end function

    }
}
